﻿using System;
using System.Xml;
using System.Globalization;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{
	
	public class DateTimeFormater
	{
		
		public static string format(Form form, XmlNode node, string value_Renamed, TypeFormat format, bool isDeformat)
		{
			if (value_Renamed.Trim().Length <= 0)
			{
				return "";
			}
			string dateFormat = format.DateFormat;
			if (dateFormat == null)
			{
				dateFormat = "yyyy-M-d";
			}
			string timeFormat = format.TimeFormat;
			if (timeFormat == null)
			{
				timeFormat = "H:mm:ss";
			}
			if (format.NoSeconds != null && format.NoSeconds.Equals("1"))
			{
				timeFormat = timeFormat.Replace(":ss", "");
			}

			string dateTimeFormat = dateFormat + " " + timeFormat;
			dateTimeFormat = dateTimeFormat.Trim();
			
			CultureInfo locale = LocaleUtil.getLocale(format);
			SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormat, locale);
			DateTime date;
			try
			{
				date = sdf.parse( value_Renamed );
			}
			catch (System.FormatException e1)
			{
				DdddDateTime ddddDate = DdddDateTime.ddddParse( value_Renamed, dateFormat, locale );
				if( !ddddDate.Valid ){
					try {
						string tryFormat = "yyyy-M-d H:mm:ss";
						if (format.NoSeconds != null && format.NoSeconds.Equals("1")) {
							tryFormat = tryFormat.Replace(":ss", "");
						}
						SimpleDateFormat try2SDF = new SimpleDateFormat(tryFormat);
						date = try2SDF.parseExact( value_Renamed );
					}
					catch (System.FormatException e2) {
						try {
							string try3Format = "yyyy-M-d'T'H:mm:ss";
							if (format.NoSeconds != null && format.NoSeconds.Equals("1")) {
								try3Format = try3Format.Replace(":ss", "");
							}
							SimpleDateFormat try3SDF = new SimpleDateFormat(try3Format);
							date = try3SDF.parseExact( value_Renamed );
						}
						catch (System.FormatException e3) {
							try {
								string try4Format = "yyyy-M-d";
								SimpleDateFormat try4SDF = new SimpleDateFormat(try4Format);
								date = try4SDF.parseExact( value_Renamed );
							}
							catch (System.FormatException e4) {
								try {
									string try5Format = "yyyy-M";
									SimpleDateFormat try5SDF = new SimpleDateFormat(try5Format);
									date = try5SDF.parseExact( value_Renamed );
								}
								catch (System.FormatException e5) {
									try {
										string try6Format = "H:mm:ss";
										if (format.NoSeconds != null && format.NoSeconds.Equals("1")) {
											try6Format = try6Format.Replace(":ss", "");
										}
										SimpleDateFormat try6SDF = new SimpleDateFormat(try6Format);
										date = try6SDF.parseExact( value_Renamed );
									}
									catch (System.FormatException e6) {
										try {
											string try7Format = "H:mm";
											SimpleDateFormat try7SDF = new SimpleDateFormat(try7Format);
											date = try7SDF.parseExact( value_Renamed );
										}
										catch (System.FormatException e7) {
											try 
											{
												SimpleDateFormat tryYyyyMMddFormat = new SimpleDateFormat("yyyyMd");
												date = tryYyyyMMddFormat.parseExact( value_Renamed );
											}
											catch (System.FormatException e8) 
											{
												return value_Renamed;
											}
										}
									}
								}
							}
						}
					}
				}else{
					date = ddddDate.DateTime;
				}
			}
			SimpleDateFormat storeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
			string nodeValue = storeFormat.format(date);
			nodeValue = FormatUtil.formatDateTimeZ( nodeValue );

			//只在FormValueValueChangeDeformatWorker调用中进行XmlNode修改.
			//其它情况中都不应该对Xml进行修改.
			if( isDeformat )
			{
				FormUtil.updateFormattingNode(form, node, nodeValue);
			}
			return sdf.format(date);
		}
	}
}